package wnhz.com.business.shareshop.activity.mine;

import android.content.Intent;
import android.os.Build;
import android.os.Message;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.rebateshop.activity.home.ShareFriandActivity;
import wnhz.com.business.shareshop.activity.shoppingcart.CommodityPublishActivity;
import wnhz.com.business.shareshop.activity.shoppingcart.SkuSettingActivity;
import wnhz.com.business.shareshop.bean.ShoppingCartListBean;
import wnhz.com.business.shareshop.bean.YajinBean;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * 共享商城我的押金
 */
public class DepositActivity extends MyBaseActivity implements ActionBarClickListener {
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.rv_deposit)
    RecyclerView rvDeposit;//押金商品
    @BindView(R.id.yajin)
    TextView yajin;//押金

    private List<YajinBean.InfoBean.ListBean> data = new ArrayList<>();

    @Override
    protected void handleMsg(Message msg) {

    }

    public static Intent createIntent(MyBaseActivity activity) {
        Intent intent = new Intent(activity, DepositActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deposit);
        ButterKnife.bind(this);
        actionbar.setData("我的押金", R.drawable.ic_lefft_white, null, 0, "", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        loadData();
    }
    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        /**
         * 我的押金
         * @param str   token
         */
        XUtil.Post(Url.UCENTER_MYDEPOSIT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("----------------", "---------------" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("re");
                    if (re.equals("1")) {
                        Gson gson = new Gson();
                        YajinBean shoppingCartListBean = gson.fromJson(result, YajinBean.class);
                        data = shoppingCartListBean.getInfo().getList();
                        double money =0;
                        for (int i = 0; i <data.size() ; i++) {
                            money = money+Double.parseDouble(data.get(i).getGoods_price());
                        }
                        yajin.setText(shoppingCartListBean.getInfo().getPrices());
                        initdatabuy();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
    private void initdatabuy() {
        rvDeposit.setLayoutManager(new LinearLayoutManager(getApplication(), LinearLayoutManager.VERTICAL, false));
        rvDeposit.setAdapter(new BaseRVAdapter(getApplication(), data) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_deposit_goods;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
//                Glide.with(DepositActivity.this).load(data.get(position).get).into(img):
                holder.getTextView(R.id.tv_name).setText(data.get(position).getGoods_name());
                holder.getTextView(R.id.tv_color).setText(data.get(position).getSku_info());
                holder.getTextView(R.id.tv_amount).setText("  ×"+data.get(position).getGoods_nums());
                holder.getTextView(R.id.tv_newprice).setText(data.get(position).getGoods_price());
                String statuse="";
                if(data.get(position).getIs_return().equals("0")){
                    statuse="退款中";
                }else if(data.get(position).getIs_return().equals("1")){
                    statuse="已退款";
                }
                holder.getTextView(R.id.statuse).setText(statuse);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(DepositActivity.this,DepositDetailsActivity.class);
                        intent.putExtra("id",data.get(position).getOrder_goods_id());
                        intent.putExtra("statuse",data.get(position).getIs_return());
                        intent.putExtra("data",data.get(position));
                        startActivity(intent);
                    }
                });
            }
        });
    }


    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
}
